package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult save(@PathVariable("name") String name,@PathVariable("age") int age,@PathVariable("sex") String sex){
        boolean save = reviewService.save(name, age, sex);
        if (save){
            return AjaxResult.success("成功插入:1条数据");
        }else
        {
            return  AjaxResult.error("新增失败");
        }

    }

    @PostMapping("/saveData")
    public AjaxResult save2(@RequestBody Review review){
        boolean save1 = reviewService.save1(review);
        AjaxResult ajaxResult=new AjaxResult();
        if (save1){
            ajaxResult = AjaxResult.success("成功插入:1条数据");
        }else
        {
            ajaxResult = AjaxResult.error("新增失败");
        }
        return ajaxResult;
    }
    /**=========================================================删除数据=============================================*/
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Review review){
        boolean update = reviewService.update(review);
        AjaxResult ajaxResult=new AjaxResult();
        if (update){
            ajaxResult = AjaxResult.success("成功修改:1条数据");
        }else
        {
            ajaxResult = AjaxResult.error("修改失败");
        }
        return ajaxResult;
    }
    /**=========================================================修改数据=============================================*/
    @DeleteMapping("/remove/{id}")
    public AjaxResult delete(@PathVariable Long id){
        boolean delete = reviewService.delete(id);
        AjaxResult ajaxResult=new AjaxResult();
        if (delete){
            ajaxResult = AjaxResult.success("成功删除");
        }else
        {
            ajaxResult = AjaxResult.error("删除失败");
        }
        return ajaxResult;
    }
    /**=========================================================查询数据=============================================*/
    @GetMapping("/getById")
    public AjaxResult getByID(@RequestParam Long id){
        Review review = reviewService.getByID(id);
        return AjaxResult.success(review);
    }

    @GetMapping("/getDataByPage")
    public AjaxResult getDataByPage(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize){
        List<Review> list = reviewService.getDataByPage(pageNum,pageSize);
        return AjaxResult.success(list);
    }
}